package cn.shenzz.suixiang.link.leetcode_142;

import cn.shenzz.suixiang.link.ListNode;

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head, fast = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow) {
                ListNode index1 = fast;
                ListNode index2 = head;
                while (index2 != index1) {
                    index2 = index2.next;
                    index1 = index1.next;
                }
                return index1;
            }
        }
        return null;
    }
}